<?php

namespace App\Models;

class Work extends Base
{
    protected $table = 'works';


    protected $fillable = [
        'user_id', 'parent_category_id', 'category_id', 'title', 'desc', 'image_file','video_file', 'comment_num'
        , 'reason', 'sort', 'star_num', 'audit_status'
    ];

    protected $casts = [
        'image_file' => 'array',
        'video_file' => 'array',
    ];

    public function user() {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function category() {
        return $this->belongsTo(Category::class, 'category_id');
    }


    public function parent_category() {
        return $this->belongsTo(Category::class, 'parent_category_id');
    }

    public function is_star() {
        return $this->hasOne(Star::class, 'entity_id')
            ->where('type', Star::TYPE_Work);
    }


    public function setImageFileAttribute($value) {
        return $this->attributes['image_file'] = $value;
    }

    public function setVideoFileAttribute($value) {
        return $this->attributes['video_file'] = $value;
    }

    public function is_focus() {
        return $this->hasOne(Focus::class, 'target_user_id', 'user_id');
    }


}
